﻿using System;
using System.Web;

namespace OnlineManagement.Common
{
	/// <summary>
	/// 缓存相关的操作类
    /// Copyright (C) Maticsoft
	/// </summary>
	public class DataCache
	{
		/// <summary>
		/// 获取当前应用程序指定CacheKey的Cache值
		/// </summary>
		/// <param name="CacheKey"></param>
		/// <returns></returns>
		public static object GetCache(string CacheKey)
		{
			System.Web.Caching.Cache objCache = HttpRuntime.Cache;
			return objCache[CacheKey];
		}

		/// <summary>
		/// 设置当前应用程序指定CacheKey的Cache值
		/// </summary>
		/// <param name="CacheKey"></param>
		/// <param name="objObject"></param>
		public static void SetCache(string CacheKey, object objObject)
		{
			System.Web.Caching.Cache objCache = HttpRuntime.Cache;
			objCache.Insert(CacheKey, objObject);
		}

		/// <summary>
		/// 设置当前应用程序指定CacheKey的Cache值
		/// </summary>
		/// <param name="CacheKey"></param>
		/// <param name="objObject"></param>
		public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration,TimeSpan slidingExpiration )
		{
			System.Web.Caching.Cache objCache = HttpRuntime.Cache;
			objCache.Insert(CacheKey, objObject,null,absoluteExpiration,slidingExpiration);
		}

        /// <summary>
        /// 弹性过期时间，当缓存没使用 noAbsoluteExpiration 秒就过期
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="obj"></param>
        /// <param name="noAbsoluteExpiration"></param>
        public static void SetNoAbsoluteExpirationCache(string cacheKey,object obj,TimeSpan noAbsoluteExpiration) {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(cacheKey, obj, null,System.Web.Caching.Cache.NoAbsoluteExpiration,noAbsoluteExpiration);
        }
        /// <summary>
        /// cache hog base info
        /// </summary>
        /// <param name="ds"></param>
        public static void CacheHogBaseInfo(System.Data.DataSet ds)
        {
            string cachKey = PubConstant.CacheHogBaseInfoName;
            object objModel = DataCache.GetCache(cachKey);
            if (objModel == null)
            {
                int modelCache = ConfigHelper.GetConfigInt("DataCacheMins");
                DataCache.SetCache(cachKey, ds, DateTime.Now.AddMinutes(modelCache), TimeSpan.Zero);
            }
        }

        /// <summary>
        /// get hog base info
        /// </summary>
        /// <returns></returns>
        public static System.Data.DataSet GetHogBaseInfo() {
            string cachKey = PubConstant.CacheHogBaseInfoName;
            object objModel = DataCache.GetCache(cachKey);
            if(objModel is System.Data.DataSet){
                System.Data.DataSet ds = objModel as System.Data.DataSet;
                return ds;
            }
            return null;

        }

        public static void CacheBuisseData(System.Data.DataSet ds, string cachKey)
        {
            try
            {
                // string cachKey = Common.PubConstant.CacheHogBaseInfoName;
                object objModel = DataCache.GetCache(cachKey);
                if (objModel == null)
                {
                    int modelCache = ConfigHelper.GetConfigInt("DataCacheMins");
                    Common.DataCache.SetCache(cachKey, ds, DateTime.Now.AddMinutes(modelCache), TimeSpan.Zero);
                }
            }catch(Exception){
                
            }
        }

        public static System.Data.DataSet GetCacheBuisseData(string cachKey)
        {
            // string cachKey = Common.PubConstant.CacheHogBaseInfoName;
            object objModel = DataCache.GetCache(cachKey);
            if (objModel is System.Data.DataSet)
            {
                System.Data.DataSet ds = objModel as System.Data.DataSet;
                return ds;
            }
            return null;

        }
	}
}
